package com.mg.plugin.agrometeorology.common;

public class MathCalculator {
    public static double[] calcDeterminant(double[][] data) {
        //数据为空判断
        if (data == null || data.length <= 0) {
            return null;
        }
        //是否是nXn+1判断
        if (data.length != data[0].length - 1) {
            return null;
        }

        if (data.length == 2) {
            double d = data[0][0] * data[1][1] - data[0][1] * data[1][0];
            double d1 = data[0][2] * data[1][1] - data[0][1] * data[1][2];
            double d2 = data[0][0] * data[1][2] - data[0][2] * data[1][0];
            return new double[]{d1 / d, d2 / d};
        }

        return ArithmeticOfCalcFour.calculate(data);
//        int x = data.length;
//        double[][] d = getMatrix(data, data.length - 1);
//        double[][] d1 = getMatrix(data, 0);
//        double[][] d2 = getMatrix(data, 1);
//        double[][] d3 = getMatrix(data, 2);
//        double[][] d4 = getMatrix(data, 3);
//
//        double dValue = calcDeterminantValue(d);
//        dValue = dValue == 0 ? 1 : dValue;
//        double d1Value = calcDeterminantValue(d1);
//        double d2Value = calcDeterminantValue(d2);
//        double d3Value = calcDeterminantValue(d3);
//        double d4Value = calcDeterminantValue(d4);
//        double[] res = new double[x];
//        res[0] = d1Value / dValue;
//        res[1] = d2Value / dValue;
//        res[2] = d3Value / dValue;
//        res[3] = d4Value / dValue;
    }

    private static double[][] getMatrix(double[][] data, int in) {
        double[][] d = new double[data.length][data.length];
        int y = 0;
        for (int i = 0; i < d.length; i++) {
            for (int j = 0; j < d.length; j++) {
                if (j == in) {
                    d[i][j] = data[i][data.length - 1];
                } else {
                    d[i][j] = data[i][j];
                }
            }
        }
        return d;
    }

    private static double calcDeterminantValue(double[][] d) {
        double res = 0;
        res += d[0][0] * d[1][1] * d[2][2] * d[3][3];
        res -= d[0][0] * d[1][3] * d[2][2] * d[3][1];
        res += d[0][1] * d[1][0] * d[3][2] * d[2][3];
        res -= d[0][1] * d[1][2] * d[2][3] * d[3][2];

        res += d[0][2] * d[1][3] * d[2][0] * d[3][1];
        res -= d[0][2] * d[1][1] * d[2][0] * d[3][3];
        res += d[0][3] * d[1][2] * d[2][1] * d[3][0];
        res -= d[0][3] * d[1][0] * d[2][1] * d[3][2];

        res += d[0][0] * d[1][2] * d[2][3] * d[3][1];
        res -= d[0][0] * d[1][1] * d[2][3] * d[3][2];
        res += d[0][2] * d[1][0] * d[2][1] * d[3][3];
        res -= d[0][2] * d[1][3] * d[2][1] * d[3][0];

        res += d[0][3] * d[1][1] * d[2][0] * d[3][2];
        res -= d[0][3] * d[1][2] * d[2][0] * d[3][1];
        res += d[0][1] * d[1][3] * d[2][2] * d[3][0];
        res -= d[0][1] * d[1][0] * d[2][2] * d[3][3];

        res += d[0][0] * d[1][3] * d[2][1] * d[3][2];
        res -= d[0][0] * d[1][2] * d[2][1] * d[3][3];
        res += d[0][3] * d[1][0] * d[2][2] * d[3][1];
        res -= d[0][3] * d[1][1] * d[2][2] * d[3][0];

        res += d[0][1] * d[1][2] * d[2][0] * d[3][3];
        res -= d[0][1] * d[1][3] * d[2][0] * d[3][2];
        res += d[0][2] * d[1][1] * d[2][3] * d[3][0];
        res -= d[0][2] * d[1][0] * d[2][3] * d[3][1];

        return res;
    }

    public static void main(String[] args) {
//        double[] d1 = {1, 1, 1, 1};
//        double[] d2 = {1, 3, 1, 1};
//        double[] d3 = {1, 1, 3, 1};
//        double[] d4 = {1, 1, 1, 3};


        //d1
        double[] d1 = {78.518, 10.951, 8.508, 10};
        double[] d2 = {74.628, 13.781, 12.222, 10};
        double[] d3 = {65.77, 14.104, 15.555, 10};
        double[] d4 = {61.20, 14.114, 15.493, 10};

        double[][] d = {d1, d2, d3, d4};

        double d1Value = calcDeterminantValue(d);

        //d2
        d1 = new double[]{30.107, 78.518, 8.508, 10};
        d2 = new double[]{31.074, 74.628, 12.222, 10};
        d3 = new double[]{29.697, 65.77, 15.555, 10};
        d4 = new double[]{30, 114, 61.20, 15.493, 10};

        d = new double[][]{d1, d2, d3, d4};

        double d2Value = calcDeterminantValue(d);

        //d3
        d1 = new double[]{30.107, 10.951, 78.518, 10};
        d2 = new double[]{31.074, 13.781, 74.628, 10};
        d3 = new double[]{29.697, 14.104, 65.77, 10};
        d4 = new double[]{30, 114, 14.114, 61.20, 10};

        d = new double[][]{d1, d2, d3, d4};

        double d3Value = calcDeterminantValue(d);

        //d4
        d1 = new double[]{30.107, 10.951, 8.508, 78.518};
        d2 = new double[]{31.074, 13.781, 12.222, 74.628};
        d3 = new double[]{29.697, 14.104, 15.555, 65.77};
        d4 = new double[]{30, 114, 14.114, 15.493, 61.20};

        d = new double[][]{d1, d2, d3, d4};

        double d4Value = calcDeterminantValue(d);

        //d
        d1 = new double[]{30.107, 10.951, 8.508, 10};
        d2 = new double[]{31.074, 13.781, 12.222, 10};
        d3 = new double[]{29.697, 14.104, 15.555, 10};
        d4 = new double[]{30, 114, 14.114, 15.493, 10};

        d = new double[][]{d1, d2, d3, d4};

        double dValue = calcDeterminantValue(d);

        d1 = new double[]{30.107, 10.951, 8.508, 10, 78.518};
        d2 = new double[]{31.074, 13.781, 12.222, 10, 74.628};
        d3 = new double[]{29.697, 14.104, 15.555, 10, 65.77};
        d4 = new double[]{30, 114, 14.114, 15.493, 10, 61.20};

        d = new double[][]{d1, d2, d3, d4};
        ArithmeticOfCalcFour.calculate(d);

//        double[] d1 = {1, 3, -1, 2};
//        double[] d2 = {0, 2, 1, -1};
//        double[] d3 = {0, 0, 8, -10};
//        double[] d4 = {0, 0, 0, 2.5};

//        double[] d1 = {1, 2, -4};
//        double[] d2 = {-2, 2, 1};
//        double[] d3 = {-3, 4, -2};
//        double[] d4 = {1, 1, 1, 3};
//        double[][] d = {d1, d2, d3, d4};
//        double[] d1 = {3, 1};
//        double[] d2 = {-5, 1};
//        double[] d3 = {2, 0, 1, -1};
//        double[] d4 = {1, -5, 3, -3};
//        double[][] d = {d1, d2};
        System.out.println("dValue:" + dValue + "\td1Value:" + d1Value + "\td2Value:" + d2Value + "\td3Value:" + d3Value + "\td4Value:" + d4Value);
        System.out.println("d1Value:" + d1Value / dValue + "\td2Value:" + d2Value / dValue + "\td3Value:" + d3Value / dValue + "\td4Value:" + d4Value / dValue);
    }
}
